package com.xzj.admin.flow.controller;

import cn.hutool.core.util.ObjectUtil;
import com.xzj.admin.flow.domain.entity.FlowManager;
import com.xzj.admin.flow.domain.vo.FlowReqVo;
import com.xzj.admin.flow.domain.vo.FlowRespVo;
import com.xzj.admin.flow.service.FlowManagerService;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.id.gen.DefaultIdHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @program: xzj-admin
 * @ClassName: FlowManagerController
 * @description: 流程管理;
 * @author: XiaZunJun
 * @create: 2024-9-27
 */
@Tag(name = "流程管理对象功能接口")
@RestController
public class FlowManagerController {
    @Resource
    private FlowManagerService flowManagerService;

    /**
     * 通过ID查询单条数据
     *
     * @param flowId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{flowId}")
    public FlowManager queryById(@PathVariable(name = "flowId") String flowId) {
        return flowManagerService.queryById(flowId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<FlowManager> pageQuery(@RequestBody PageReq<FlowManager> pageReq) {
        return flowManagerService.pageQuery(pageReq);
    }

    /**
     * 新增数据
     *
     * @param flowManager 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public FlowManager addOrUpdate(@RequestBody FlowManager flowManager) {
        if (ObjectUtil.isNotEmpty(flowManager.getFlowId())) {
            flowManagerService.update(flowManager);
        } else {
            flowManager.setFlowId(DefaultIdHelper.nextIdStr());
            flowManagerService.insert(flowManager);
        }
        return flowManager;
    }

    /**
     * 通过主键删除数据
     *
     * @param flowId 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据")
    @GetMapping("/deleteById/{flowId}")
    public boolean deleteById(@PathVariable(name = "flowId") String flowId) {
        return flowManagerService.deleteById(flowId);
    }

    @Operation(summary = "添加流程图")
    @PostMapping("/addFlowChart")
    public void addFlowChart(@RequestBody FlowReqVo flowReqVo) {
        flowManagerService.addFlowChart(flowReqVo);
    }

    @Operation(summary = "查询流程图")
    @GetMapping("/queryFlowChart/{flowId}")
    public FlowRespVo queryFlowChart(@PathVariable(name = "flowId") String flowId) {
        return flowManagerService.queryFlowChart(flowId);
    }
}
